package com.todo.member.application.convert;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.member.application.entity.bo.MemberTaskBO;
import com.todo.member.application.entity.bo.RelationRewardBO;
import com.todo.member.application.entity.condition.MemberTaskConditionBO;
import com.todo.member.common.enums.MemberTaskCirculateTypeEnum;
import com.todo.member.common.enums.MemberTaskClassifyEnum;
import com.todo.member.common.enums.MemberTaskStatusEnum;
import com.todo.member.infrastructure.entity.condition.MemberTaskCondition;
import com.todo.member.infrastructure.entity.po.MemberTaskPO;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author azhebuxing
 * @date 2025/3/5 02:27
 * @description
 */
public class TaskConvert {
    public static MemberTaskPO convertToMemberTaskPO(MemberTaskBO memberTaskBO) {
        if (memberTaskBO == null) {
            return null;
        }
        MemberTaskPO memberTaskPO = new MemberTaskPO();
        memberTaskPO.setId(memberTaskBO.getId());
        memberTaskPO.setTitle(memberTaskBO.getTitle());
        memberTaskPO.setDesc(memberTaskBO.getDesc());
        memberTaskPO.setTaskClassify(memberTaskBO.getTaskClassify() == null ? null : memberTaskBO.getTaskClassify().getType());
        memberTaskPO.setFinishRule(memberTaskBO.getFinishRule());
        memberTaskPO.setDisplayRule(memberTaskBO.getDisplayRule());
        memberTaskPO.setTaskType(memberTaskBO.getTaskType() == null ? null : memberTaskBO.getTaskType().getType());
        memberTaskPO.setStartTime(memberTaskBO.getStartTime());
        memberTaskPO.setEndTime(memberTaskBO.getEndTime());
        memberTaskPO.setRelationReward(JSONObject.toJSONString(memberTaskBO.getRelationReward()));
        memberTaskPO.setRewardCalLogic(memberTaskBO.getRewardCalLogic());
        memberTaskPO.setStatus(memberTaskBO.getStatus() == null ? null : memberTaskBO.getStatus().getType());
        return memberTaskPO;
    }

    public static MemberTaskBO convertToMemberTaskBO(MemberTaskPO memberTaskPO) {
        if (memberTaskPO == null) {
            return null;
        }
        MemberTaskBO memberTaskBO = new MemberTaskBO();
        memberTaskBO.setId(memberTaskPO.getId());
        memberTaskBO.setTitle(memberTaskPO.getTitle());
        memberTaskBO.setDesc(memberTaskPO.getDesc());
        memberTaskBO.setTaskClassify(MemberTaskClassifyEnum.getByType(memberTaskPO.getTaskClassify()));
        memberTaskBO.setFinishRule(memberTaskPO.getFinishRule());
        memberTaskBO.setDisplayRule(memberTaskPO.getDisplayRule());
        memberTaskBO.setTaskType(MemberTaskCirculateTypeEnum.getByType(memberTaskPO.getTaskType()));
        memberTaskBO.setStartTime(memberTaskPO.getStartTime());
        memberTaskBO.setEndTime(memberTaskPO.getEndTime());
        memberTaskBO.setRelationReward(JSONObject.parseArray(memberTaskPO.getRelationReward(), RelationRewardBO.class));
        memberTaskBO.setRewardCalLogic(memberTaskPO.getRewardCalLogic());
        memberTaskBO.setStatus(MemberTaskStatusEnum.getByType(memberTaskPO.getStatus()));
        return memberTaskBO;
    }

    public static MemberTaskCondition convertToMemberTaskCondition(MemberTaskConditionBO memberTaskConditionBO) {
        MemberTaskCondition memberTaskCondition = new MemberTaskCondition();
        memberTaskCondition.setId(memberTaskConditionBO.getId());
        memberTaskCondition.setTitle(memberTaskConditionBO.getTitle());
        memberTaskCondition.setTaskClassify(memberTaskConditionBO.getTaskClassify() == null ? null : memberTaskConditionBO.getTaskClassify().getType());
        memberTaskCondition.setTaskType(memberTaskConditionBO.getTaskType() == null ? null : memberTaskConditionBO.getTaskType().getType());
        memberTaskCondition.setStartTime(memberTaskConditionBO.getStartTime());
        memberTaskCondition.setEndTime(memberTaskConditionBO.getEndTime());
        memberTaskCondition.setStatus(memberTaskConditionBO.getStatus() == null ? null : memberTaskConditionBO.getStatus().getType());
        memberTaskCondition.setPageIndex(memberTaskConditionBO.getPageNum());
        memberTaskCondition.setPageSize(memberTaskConditionBO.getPageSize());
        return memberTaskCondition;
    }

    public static PageResult<MemberTaskBO> convertToMemberTaskPageResult(PageResult<MemberTaskPO> pageResult) {
        if (pageResult == null) {
            return PageResult.of(Lists.newArrayList(), 0, 0, 0);
        }
        if (CollectionUtils.isEmpty(pageResult.getResults())) {
            return PageResult.of(Lists.newArrayList(), pageResult.getPageNum(), pageResult.getPageSize(), pageResult.getTotal());
        }
        List<MemberTaskBO> collect = pageResult.getResults().stream().map(TaskConvert::convertToMemberTaskBO).collect(Collectors.toList());
        return PageResult.of(collect, pageResult.getPageNum(), pageResult.getPageSize(), pageResult.getTotal());
    }
}
